<?php

/**
 * Description of CriptyHelper
 *
 * @author igor
 */
class SecurityEncryptionHelper {

    public static function encrypt($word) {

        $config = parse_ini_file("system/config/config.ini", true);

        if (isset($config['seguranca']['cod_base']) && isset($config['seguranca']['frase_segura'])) {
            
            $frase = (string) $config['seguranca']['frase_segura'];
            $frase = md5(sha1($frase));
            
            $word .= $frase;
            $s = strlen($word) + 1;
            $nw = "";
            $n = (int) $config['seguranca']['cod_base'];
            for ($x = 1; $x < $s; $x++) {
                $m = $x * $n;
                if ($m > $s) {
                    $nindex = $m % $s;
                } else if ($m < $s) {
                    $nindex = $m;
                }
                if ($m % $s == 0) {
                    $nindex = $x;
                }
                $nw = $nw . $word[$nindex - 1];
            }
            return $nw;
        } else {
            return false;
        }
    }

    public static function decrypt($word) {
        
        
        $config = parse_ini_file("system/config/config.ini", true);

        if (isset($config['seguranca']['cod_base']) && isset($config['seguranca']['frase_segura'])) {

            $frase = (string) $config['seguranca']['frase_segura'];
            $frase = md5(sha1($frase));

            $s = strlen($word) + 1;
            $nw = "";
            $n = (int) $config['seguranca']['cod_base'];
            for ($y = 1; $y < $s; $y++) {
                $m = $y * $n;
                if ($m % $s == 1) {
                    $n = $y;
                    break;
                }
            }
            for ($x = 1; $x < $s; $x++) {
                $m = $x * $n;
                if ($m > $s) {
                    $nindex = $m % $s;
                } else if ($m < $s) {
                    $nindex = $m;
                }
                if ($m % $s == 0) {
                    $nindex = $x;
                }
                $nw = $nw . $word[$nindex - 1];
            }
            $t = strlen($nw) - strlen($frase);
            return substr($nw, 0, $t);
        } else {
            return false;
        }
    }

}
?>